package com.mallify.admin.controller;

import com.mallify.admin.service.SystemCityService;
import com.mallify.common.model.system.SystemCity;
import com.mallify.common.request.SystemCityRequest;
import com.mallify.common.request.SystemCitySearchRequest;
import com.mallify.common.response.Result;
import com.mallify.common.vo.SystemCityTreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 城市表 前端控制器
 * +----------------------------------------------------------------------
 * | HANSON [ HANSON赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Licensed HANSON并不是自由软件，未经许可不能去掉HANSON相关版权
 * +----------------------------------------------------------------------
 * | Author: HANSON Team <hanson0622@126.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/system/city")
@Api(tags = "城市管理")
public class SystemCityController {

    @Resource
    private SystemCityService systemCityService;

    /**
     * 分页城市列表
     *
     * @param request 搜索条件
     */
    @PreAuthorize("hasAuthority('admin:system:city:list')")
    @ApiOperation(value = "获取城市列表")
    @GetMapping(value = "/list")
    public Result<Object> getList(@Validated SystemCitySearchRequest request) {
        return Result.success(systemCityService.getList(request));
    }

    /**
     * 修改城市
     *
     * @param id      城市id
     * @param request 修改参数
     */
    @PreAuthorize("hasAuthority('admin:system:city:update')")
    @ApiOperation(value = "修改")
    @PostMapping(value = "/update")
    public Result<String> update(@RequestParam Integer id, @RequestBody @Validated SystemCityRequest request) {
        if (Boolean.TRUE.equals(systemCityService.update(id, request))) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 修改状态
     * @param id 城市id
     * @param status 状态
     */
    @PreAuthorize("hasAuthority('admin:system:city:update:status')")
    @ApiOperation(value = "修改状态")
    @PostMapping(value = "/update/status")
    public Result<String> updateStatus(@RequestParam Integer id, @RequestParam Boolean status) {
        if (Boolean.TRUE.equals(systemCityService.updateStatus(id, status))) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 城市详情
     * @param id 城市id
     */
    @PreAuthorize("hasAuthority('admin:system:city:info')")
    @ApiOperation(value = "城市详情")
    @GetMapping(value = "/info")
    public Result<SystemCity> info(@RequestParam(value = "id") Integer id) {
        return Result.success(systemCityService.getById(id));
    }

    /**
     * 获取tree结构的列表
     */
    @ApiOperation(value = "获取tree结构的列表")
    @GetMapping(value = "/list/tree")
    public Result<List<SystemCityTreeVo>> getListTree() {
        return Result.success(systemCityService.getListTree());
    }
}